<template>
  <el-dialog
    :title="!dataForm.id ? '新增泵站报警阀值' : '修改泵站报警阀值'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="missionPart"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <div class="tablePart">
        <el-form-item label="泵站：" prop="equipmentId" class="fullList">
          <el-select filterable v-model="dataForm.equipmentId" disabled>
            <el-option
              v-for="gItem in stationOptions"
              :key="gItem.id"
              :label="gItem.flowMeterName"
              :value="gItem.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="流量报警最大值：" prop="flowUpperLimit">
          <el-input-number v-model="dataForm.flowUpperLimit" label="设定峰值" />
        </el-form-item>
        <el-form-item label="流量报警最小值：" prop="flowLowerLimit">
          <el-input-number v-model="dataForm.flowLowerLimit" label="设定峰值" />
        </el-form-item>
        <el-form-item
          label="发送报警信息："
          prop="isFlowAlarm"
          class="fullList"
        >
          <el-switch
            v-model="dataForm.isFlowAlarm"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            >发送流量报警信息</el-switch
          >
        </el-form-item>
        <el-form-item label="压力报警最大值：" prop="pressUpperLimit">
          <el-input-number
            v-model="dataForm.pressUpperLimit"
            label="设定峰值"
          />
        </el-form-item>
        <el-form-item label="压力报警最小值：" prop="pressLowerLimit">
          <el-input-number
            v-model="dataForm.pressLowerLimit"
            label="设定峰值"
          />
        </el-form-item>
        <el-form-item
          label="发送报警信息："
          prop="isPressAlarm"
          class="fullList"
        >
          <el-switch
            v-model="dataForm.isPressAlarm"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            >发送压力报警信息</el-switch
          >
        </el-form-item>
        <el-form-item
          label="流量计断电时报警："
          prop="isOutageAlarm"
          class="fullList"
        >
          <el-switch
            v-model="dataForm.isOutageAlarm"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      checked: true,
      warningCheck: true,
      stationOptions: [], // 泵站信息
      dataForm: {
        equipmentId: null, // 设备id
        flowLowerLimit: null, // 流量下限值
        flowUpperLimit: null, // 流量上限值
        id: 0,
        isFlowAlarm: 0, // 是否流量报警
        isOutageAlarm: 0, // 是否断电报警
        isPressAlarm: 0, // 是否压力报警
        pressLowerLimit: null, // 压力下限值
        pressUpperLimit: null // 压力上限值
      }
    };
  },
  mounted() {
    this.getStationOption();
  },
  methods: {
    init(equipment, id) {
      this.dataForm.equipmentId = parseInt(equipment);
      this.dataForm.id = id || null;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/cms/tbAlarmMonitor/info/${this.dataForm.id}`
            ),
            method: "get"
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.equipmentId = data.tbAlarmMonitor.equipmentId;
              this.dataForm.flowLowerLimit = data.tbAlarmMonitor.flowLowerLimit;
              this.dataForm.flowUpperLimit = data.tbAlarmMonitor.flowUpperLimit;
              this.dataForm.isFlowAlarm = data.tbAlarmMonitor.isFlowAlarm + "";
              this.dataForm.isOutageAlarm =
                data.tbAlarmMonitor.isOutageAlarm + "";
              this.dataForm.isPressAlarm =
                data.tbAlarmMonitor.isPressAlarm + "";
              this.dataForm.pressLowerLimit =
                data.tbAlarmMonitor.pressLowerLimit;
              this.dataForm.pressUpperLimit =
                data.tbAlarmMonitor.pressUpperLimit;
            }
          });
        } else {
          this.dataForm.equipmentId = parseInt(equipment);
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      // this.$refs["dataForm"].validate(valid => {
      //   if (valid) {
      this.$http({
        url: this.$http.adornUrl(
          `/cms/tbAlarmMonitor/${!this.dataForm.id ? "save" : "update"}`
        ),
        method: "post",
        data: this.$http.adornData({
          equipmentId: this.dataForm.equipmentId, // 设备id
          flowLowerLimit: this.dataForm.flowLowerLimit, // 流量下限值
          flowUpperLimit: this.dataForm.flowUpperLimit, // 流量上限值
          id: this.dataForm.id || undefined,
          isFlowAlarm: parseInt(this.dataForm.isFlowAlarm), // 是否流量报警
          isOutageAlarm: parseInt(this.dataForm.isOutageAlarm), // 是否断电报警
          isPressAlarm: parseInt(this.dataForm.isPressAlarm), // 是否压力报警
          pressLowerLimit: this.dataForm.pressLowerLimit, // 压力下限值
          pressUpperLimit: this.dataForm.pressUpperLimit // 压力上限值
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
      //   }
      // });
    },
    // 获取泵站信息
    getStationOption() {
      this.$http({
        url: this.$http.adornUrl(`/cms/tbEquipment/combo`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.stationOptions = data.list;
        } else {
          this.stationOptions = [];
        }
      });
    }
  }
};
</script>
